//
//  DownloadHandler.m
//  Download
//
//  Created by Maurice on 2021/7/17.
//

#import "Downloader.h"

@interface Downloader ()

@property(nonatomic, strong) NSURLSession *session;

@end

@implementation Downloader

- (instancetype)init
{
    self = [super init];
    if (self) {
        _session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    }
    return self;
}

- (void)downloadWithURL:(NSURL *)URL
         successHandler:(void (^)(NSURL *URL, NSURLResponse * _Nullable response))successHandler
          failureHadler:(void (^)(NSError * error))failureHadler
{
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    NSURLSessionDownloadTask *task = [_session downloadTaskWithRequest:request
                                                     completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"response: %@ error: %@", response, error);
    }];
    [task resume];
}

@end

